﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Trippy.Animation;
using Trippy.CollidableGameComponents.Agents;
using Trippy.Levels;

namespace Trippy.CollidableGameComponents.Events
{
    public class Event : CollidableGameComponent
    {
        public static Texture2D SpriteSheet { get; private set; }

        protected override void LoadContent()
        {
            if (SpriteSheet == null) SpriteSheet = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Events/event");
            if (Font == null) Font = Game.Content.Load<SpriteFont>("Fonts/UI");

            Sprite = new AnimatedSprite(this, SpriteSheet, SpriteSheet.Height, SpriteSheet.Width, 0, 0, 0, 0, 1, 0f);
            base.LoadContent();
        }

        // only draw events if in debug mode
#if DEBUG
        public override void Draw(GameTime gameTime)
        {
            StandardDraw(gameTime);
            base.Draw(gameTime);
        }
#endif

        public bool Frozen { get; set; }

        public delegate void EventHandler();

        public static SpriteFont Font { get; protected set; }

        public EventHandler TopCollision { get; protected set; }
        public EventHandler BottomCollision { get; protected set; }
        public EventHandler LeftCollision { get; protected set; }
        public EventHandler RightCollision { get; protected set; }

        public String Text { get; set; }

        public Event(Level level)
            : base(level)
        {
            Frozen = false;
            Text = "";
        }

        public virtual void AssignHandlers(EventHandler top, EventHandler bottom, EventHandler left, EventHandler right)
        {
            TopCollision = top;
            BottomCollision = bottom;
            LeftCollision = left;
            RightCollision = right;
        }

        public virtual void AssignHandler(EventHandler handler)
        {
            AssignHandlers(handler, handler, handler, handler);
        }

        protected override void CollisionHandler(CollidableGameComponent component, CollisionSide side)
        {
            if (Frozen) return;
            if (component is Hero)
            {
                switch (side)
                {
                    case CollisionSide.Top:
                        if (TopCollision != null) TopCollision();
                        break;
                    case CollisionSide.Right:
                        if (RightCollision != null) RightCollision();
                        break;
                    case CollisionSide.Bottom:
                        if (BottomCollision != null) BottomCollision();
                        break;
                    case CollisionSide.Left:
                        if (LeftCollision != null) LeftCollision();
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("side");
                }
            }
            base.CollisionHandler(component, side);
        }

    }

}
